package com.csii.adhoc.dataready.serviceImpl;

import com.csii.adhoc.dataready.TDataConstant;
import com.csii.adhoc.dataready.TDataNickName;
import com.csii.adhoc.dataready.TDataParamPo;
import com.csii.adhoc.dataready.mapper.TDataParamMapper;
import com.csii.adhoc.dataready.service.TDataParamService;
import com.csii.adhoc.exception.ParamValidityException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.util.List;
import java.util.UUID;

/**
 * @description: 数据集接口实现类
 * @author: zyliu
 * @create: 2021-01-13 09:32
 */
@Service("tDataParamService")
@Slf4j
public class TDataParamServiceImpl implements TDataParamService {

    @Autowired
    TDataParamMapper tDataParamMapper;

    @Override
    @Transactional
    public void tDataParamMaintain(TDataParamPo po, String optMode) {
        //检查上传的值

        switch (optMode) {
            case TDataConstant.OPT_MODE_ADD:
                po.setCreatedBy(po.getUpdatedBy());
                insertOneTDataParam(po);
                break;
            case TDataConstant.OPT_MODE_UPDATE:
                if (po.getUpdatedBy().isEmpty() ||
                        po.getUpdatedBy() == null) {
                    log.error("更新柜员号不能为空，UpdatedBy:" + po.getUpdatedBy());
                    throw new ParamValidityException("更新柜员号不能为空，UpdatedBy:" + po.getUpdatedBy());
                }
                updateTDataParamByPrimaryKey(po);
                break;
            case TDataConstant.OPT_MODE_DELETE:
                deleteByModelId(po.getModelId());
                break;
            default:
                log.error("optMode值不合法:" + optMode);
                throw new ParamValidityException("操作标志不合法:" + optMode);
            }
    }

    @Transactional
    @Override
    public void insertOneTDataParam(TDataParamPo tDataParamPo) {
        if (StringUtils.isEmpty(tDataParamPo.getModelId())) {
            tDataParamPo.setModelId(UUID.randomUUID().toString().replace("-", ""));
        }
        tDataParamPo.setCreatedDt(tDataParamPo.getUpdatedDt());
        tDataParamMapper.insertOneTDataParam(tDataParamPo);
    }

    @Transactional
    @Override
    public int updateTDataParamByPrimaryKey(TDataParamPo tDataParamPo) {
        return tDataParamMapper.updateByPrimaryKey(tDataParamPo);
    }

    @Transactional
    @Override
    public int deleteByModelId(String modelId) {
        return tDataParamMapper.deleteByModelId(modelId);
    }

    @Transactional
    @Override
    public int deleteBySeqno(String modelId,int seqno) {
        return tDataParamMapper.deleteBySeqno(modelId,seqno);
    }

    @Override
    public TDataParamPo selectByPrimaryKey(String modelId) {
        return tDataParamMapper.selectByPrimaryKey(modelId);
    }

    @Override
    public List<TDataParamPo> selectListTDataParam(String modelId) {
        return tDataParamMapper.selectListTDataParam(modelId);
    }


    @Override
    @Transactional
    public void saveNickName(TDataNickName tDataNickName) {
        if (tDataParamMapper.checkIsExistNickName(tDataNickName) > 0) {
            tDataParamMapper.updateNickNameInfo(tDataNickName);
        } else {
            tDataParamMapper.saveNickName(tDataNickName);
        }
    }

    @Override
    public List<TDataNickName> queryExistNickName(TDataNickName tDataNickName) {
        return tDataParamMapper.queryExistNickName(tDataNickName);
    }

    @Override
    public void updateNickNameInfo(TDataNickName tDataNickName) {
        if (tDataParamMapper.checkIsExistNickName(tDataNickName) > 0) {
            tDataParamMapper.updateNickNameInfo(tDataNickName);
        } else {
            tDataParamMapper.saveNickName(tDataNickName);
        }
    }
}
